<?php
Prado::using('Application.common.common');
class ManageUsers extends TPage
{
	private $_maxPage = 1;
	private $_currentPage = 1;
	private $_queryParams = array("p","st","sb","q");
	
	public function setMaxPage($value)
	{
		$this->_maxPage = TPropertyValue::ensureInteger($value);
	}
	
	public function getMaxPage()
	{
		return $this->_maxPage;
	}

	public function setCurrentPage($value)
	{
		$this->_currentPage = TPropertyValue::ensureInteger($value);
	}

	public function getCurrentPage()
	{
		return $this->_currentPage;
	}
	
	public function getQueryParameters()
	{
		return $this->_queryParams;
	}
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = "Admin | Manage Users";
		$this->AdminMenu->lnkManageUser->CssClass = "active";
		$this->CurrentPage = ($this->Request->contains('p')) ? intval($this->Request['p']) : 1;
		
		if (!$this->IsPostBack)
		{			
			$this->populateUsers();
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'du')	
					$this->Notice->Text = $this->Application->getModule('message')->translate('DELETE_SUCCESS','User','');				
			}	
		}
	}
	
	protected function populateUsers()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "user_status >= 1";				
		$criteria->OrdersBy["user_name"] = "asc";							
		$activeRecord = Prado::createComponent("UserRecord");
		$this->ItemList->VirtualItemCount = count($activeRecord->finder()->findAll($criteria));
		$totalItems = $this->ItemList->VirtualItemCount;
		$this->MaxPage = ceil($this->ItemList->VirtualItemCount/$this->ItemList->PageSize);
		if ($this->CurrentPage > $this->MaxPage) $this->CurrentPage = $this->MaxPage;
		$limit = $this->ItemList->PageSize;
		$offset = ($this->CurrentPage-1) * $limit;

		if ($offset + $limit > $this->ItemList->VirtualItemCount)
			$limit = $this->ItemList->VirtualItemCount - $offset;

		$criteria->Limit = $limit;
		$criteria->Offset = $offset;
		$items = $activeRecord->finder()->findAll($criteria);
		$this->ItemList->DataSource = $items;
		$this->ItemList->DataBind();
		$this->lblTotal->Text=$totalItems." Users <font color='black' style='text-transform:capitalize;'>| Page</font> " . $this->CurrentPage . " <font color='black' style='text-transform:capitalize;'>of</font> ".$this->MaxPage;	
		if (count($items) <= 0)
		{
			$this->lblTotal->Text="There is no user.";
			$this->lblTotal->Style="text-transform:lower; margin-left:40px;";
		}
	}
	
	protected function list_ItemCreated($sender, $param)
	{
		if ($param->Item->ItemType == "Item" || $param->Item->ItemType == "AlternatingItem")
		{
			if ($param->Item->Data)
			{		
				if ($param->Item->Data->Status == 2)
				{
					$param->Item->colInfo->lblUserType->Text = "(admin)";
					$param->Item->colInfo->lblUserType->Visible = "true";
					$param->Item->colInfo->lblUserType->Style = "color:blue";
				}		
			}
		}
	}	
	
	protected function list_ItemCommand($sender, $param)
	{
		switch($param->CommandName)
		{
			case "delete":			
				$userRecord = UserRecord::finder()->findByPk($param->Item->colButton->txtUserID->Value);
				$userRecord->Status = "-1";
				$userRecord->ModifyBy = $this->Application->User->ID;
				$userRecord->ModifyDate = time();										
				$userRecord->save();	
				$this->Response->redirect($this->Service->ConstructUrl('admin.ManageUsers', array('m'=>'du')));				
				break;		
			default:
				break;
		}
	}
}

?>